/*
 * by cansik / Florian
 * Datum: 20.08.2010
 * Zeit: 08:14
 * 
 *
 */
using HtmlAgilityPack;
using System;
using System.Text;
using System.Xml;

namespace TM.Unofficial.PersonalMessageSystem
{
	
	/// <summary>
	/// ReceivedMessage in the Inbox
	/// </summary>
	public class ReceivedMessage : PersonalMessage
	{
		internal Unofficial.Network.VBrowser browser;
		
		/// <summary>
		/// Message ID
		/// </summary>
		public string ID{get; internal set;}
		/// <summary>
		/// Member who sends the message
		/// </summary>
		public string Sender{get; internal set;}
		/// <summary>
		/// Shows the status of the message
		/// </summary>
		public PersonalMessageStatus Status{get; internal set;}
		/// <summary>
		/// marked as Favorite Message
		/// </summary>
		public bool Favorite{get; internal set;}
		/// <summary>
		/// Incoming Date
		/// </summary>
		public DateTime Date{get; internal set;}
		
		/// <summary>
		/// Content of the message
		/// </summary>
		public override string Text {
			get
			{
				//Downloads the text if it does not exist
				if (base.Text == null)
				{
					base.Text = this.Open();
				}
				return base.Text;
			}
			set { base.Text = value; }
		}
		
		/// <summary>
		/// Create new Received Message
		/// </summary>
		public ReceivedMessage()
		{
		}
		
		/// <summary>
		/// Opens the message and loads the text
		/// </summary>
		/// <returns>Text of Message</returns>
		private string Open()
		{
			//parse result
			HtmlDocument doc = new HtmlDocument();
			doc.LoadHtml(browser.GetRequest(new Uri("http://thats-me.ch/tm-briefli/index.php?s=1&filter=&msg=" + this.ID + "#msg"), false));
			HtmlNode lesen = doc.GetElementbyId("lesen");
			
			//maybe we could do it better...
			string searchstring = "postmain\">";
			string text = lesen.InnerHtml.Remove(0, lesen.InnerHtml.IndexOf(searchstring) + searchstring.Length);
			text = text.Remove(text.IndexOf("<hr noshade=")).Trim();
				
			return text;
		}
	}
}
